package com.blog.cmrpersonalblog.enums;

/**
 * 文章状态枚举
 */
public enum ArticleStatus {
    
    /**
     * 草稿状态
     */
    DRAFT(0, "草稿"),
    
    /**
     * 已发布状态
     */
    PUBLISHED(1, "已发布"),
    
    /**
     * 已删除状态
     */
    DELETED(2, "已删除"),
    
    /**
     * 待审核状态
     */
    PENDING_REVIEW(3, "待审核"),
    
    /**
     * 审核拒绝状态
     */
    REVIEW_REJECTED(4, "审核拒绝"),
    
    /**
     * 已下架状态
     */
    WITHDRAWN(5, "已下架");
    
    private final Integer code;
    private final String description;
    
    ArticleStatus(Integer code, String description) {
        this.code = code;
        this.description = description;
    }
    
    public Integer getCode() {
        return code;
    }
    
    public String getDescription() {
        return description;
    }
    
    /**
     * 根据状态码获取枚举
     * 
     * @param code 状态码
     * @return 对应的枚举值
     */
    public static ArticleStatus fromCode(Integer code) {
        if (code == null) {
            return null;
        }
        
        for (ArticleStatus status : ArticleStatus.values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        
        throw new IllegalArgumentException("Invalid article status code: " + code);
    }
    
    /**
     * 检查是否为已发布状态
     */
    public boolean isPublished() {
        return this == PUBLISHED;
    }
    
    /**
     * 检查是否为草稿状态
     */
    public boolean isDraft() {
        return this == DRAFT;
    }
    
    /**
     * 检查是否为已删除状态
     */
    public boolean isDeleted() {
        return this == DELETED;
    }
    
    /**
     * 检查是否为待审核状态
     */
    public boolean isPendingReview() {
        return this == PENDING_REVIEW;
    }
    
    /**
     * 检查是否为审核拒绝状态
     */
    public boolean isReviewRejected() {
        return this == REVIEW_REJECTED;
    }
    
    /**
     * 检查是否为已下架状态
     */
    public boolean isWithdrawn() {
        return this == WITHDRAWN;
    }
    
    /**
     * 检查是否可以被用户查看（已发布状态）
     */
    public boolean isViewable() {
        return this == PUBLISHED;
    }
    
    /**
     * 检查是否可以被下架（只有已发布的文章可以下架）
     */
    public boolean canWithdraw() {
        return this == PUBLISHED;
    }
    
    /**
     * 检查是否可以重新发布
     * 草稿、已下架、审核拒绝的文章可以重新发布
     */
    public boolean canPublish() {
        return this == DRAFT || this == WITHDRAWN || this == REVIEW_REJECTED;
    }
    
    /**
     * 检查是否可以提交审核
     * 草稿、审核拒绝的文章可以提交审核
     */
    public boolean canSubmitForReview() {
        return this == DRAFT || this == REVIEW_REJECTED;
    }
    
    /**
     * 检查是否可以审核通过
     * 待审核状态的文章可以审核通过
     */
    public boolean canApprove() {
        return this == PENDING_REVIEW;
    }
    
    /**
     * 检查是否可以审核拒绝
     * 待审核状态的文章可以审核拒绝
     */
    public boolean canReject() {
        return this == PENDING_REVIEW;
    }
    
    /**
     * 检查是否为终态（已删除）
     */
    public boolean isFinalState() {
        return this == DELETED;
    }
    
    /**
     * 检查是否需要审核（根据配置可能需要审核的状态）
     */
    public boolean needsReview() {
        return this == PENDING_REVIEW;
    }
}
